package com.RecipeBook.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//import com.RecipeBook.common.DataHelper.OpenHelper;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DataManager {

	private static final String DATABASE_NAME = "recipebook";
	private static final int DATABASE_VERSION = 1;
	public static final String TABLE_NAME = "recipes";
	
	private Context context;
	private SQLiteDatabase db;
	private SQLiteStatement insrtStmt;
	private static final String INSERT = "insert into " + TABLE_NAME + "(name) values (?)";
	
	public DataManager(Context context) {
		this.context = context;
		OpenHelper openHelper = new OpenHelper(this.context);
		this.db = openHelper.getWritableDatabase();
		insrtStmt = this.db.compileStatement(INSERT);
	}
	
	public List<String> selectAll(String table) {
		List<String> list = new ArrayList<String>();
		Cursor cursor = this.db.query(table, new String[] { "name" }, 
				null, null, null, null, "name desc");
		if (cursor.moveToFirst()) {
			do {
				list.add(cursor.getString(0)); 
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return list;
	}
	
	private static class OpenHelper extends SQLiteOpenHelper {

		OpenHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			//Scanner scan = new Scanner(CREATE_STMT_FILE);
			
			//while(scan.hasNext()) {
				db.execSQL("create table recipe(id int, name varchar[20], tags varchar[20], rating int, time int, img_key varchar[20], primary key(id));");
			//}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w("Example", "Upgrading database, this will drop tables and recreate.");
			//db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
	}
	
}
